//
//  ApiUrlProvider.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 3/7/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "ApiUrlProvider.h"

static NSString *const HTTP					= @"http";
#ifdef DEBUG
static NSString *const HTTPS				= @"http";
//static NSString *const BASE_HOST			= @"11870.org";
//static NSString *const BASE_SUBHOST			= @"api.apachex";
//static NSString *const BASE_SUBHOST			= @"matteo";
//static NSString *const BASE_SUBHOST			= @"pablo";
#else
static NSString *const HTTPS				= @"https";
#endif
static NSString *const BASE_HOST			= @"11870.com";
static NSString *const BASE_SUBHOST			= @"api";

static NSString *const BASE_SLUG			= @"/api/v2";
static NSString *const PRO_SLUG				= @"/pro";
static NSString *const SEARCH_SLUG			= @"/search";
static NSString *const SERVICE_REVIEWS_SLUG	= @"/site-reviews";
static NSString *const SERVICE_DETAILS_SLUG	= @"/site-details";
static NSString *const SERVICE_MEDIA_SLUG	= @"/site-media";
static NSString *const CATEGORIES_SLUG		= @"/categories";
static NSString *const ATTRIBUTES_SLUG		= @"/categories";
static NSString *const CHECKIN_USER_SLUG	= @"/checkins/user";
static NSString *const CHECKIN_SERVICE_SLUG	= @"/checkins/site";
static NSString *const USERS_SLUG			= @"/users";
static NSString *const USER_SERVICES_SLUG	= @"/sites";
static NSString *const CONTACTS_SLUG		= @"/contacts";
static NSString *const USER_ACTIVITY_SLUG	= @"/activity";
static NSString *const SOCIAL_ACTIVITY_SLUG	= @"/social-activity";
static NSString *const LISTS_SLUG			= @"/lists";
static NSString *const TAGS_SLUG			= @"/tags";
static NSString *const OFFERS_SLUG          = @"/offers";
static NSString *const OFFERS_USER_SLUG     = @"/offers/user";
static NSString *const OFFERS_SITE_SLUG     = @"/offers/site";


static NSString *const API_DOMAIN_TEMPLATE		= @"%@://%@";
static NSString *const API_SUBDOMAIN_TEMPLATE	= @"%@://%@.%@";


@implementation ApiUrlProvider

@synthesize base;
@synthesize baseDomain;
@synthesize search;
@synthesize serviceReviews;
@synthesize serviceDetails;
@synthesize serviceMedia;
@synthesize categories;
@synthesize attributes;
@synthesize userCheckin;
@synthesize serviceCheckin;
@synthesize users;
@synthesize userServices;
@synthesize contacts;
@synthesize userActivity;
@synthesize socialActivity;
@synthesize userLists;
@synthesize userTags;
@synthesize servicePage;
@synthesize offers;
@synthesize userOffers;
@synthesize siteOffers;

@synthesize protocol = baseProtocol;
@synthesize domain = baseHost;
@synthesize subDomain = baseSubHost;

- (void) build
{
	if (!baseProtocol) {
		baseProtocol = HTTP;
	}
    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
	if (!baseHost) {
        NSString *envBaseHost = [environment objectForKey:@"BASE_HOST"];
        if (envBaseHost != nil) {
            baseHost = envBaseHost;
        } else {
            baseHost = BASE_HOST;
        }
	}
	if (!baseSubHost) {
        NSString *envBaseSubHost = [environment objectForKey:@"BASE_SUBHOST"];
        if (envBaseSubHost != nil) {
            baseSubHost = envBaseSubHost;
        } else {
            baseSubHost = BASE_SUBHOST;
        }
	}
	
	apiDomain = [ApiUrl apiUrl];
	NSString *url;
	if (!baseSubHost) {
		url = [NSString stringWithFormat: API_DOMAIN_TEMPLATE, 
				baseProtocol, baseHost, nil];
	} else {
		url = [NSString stringWithFormat: API_SUBDOMAIN_TEMPLATE, 
				baseProtocol, baseSubHost, baseHost, nil];
	}
	[apiDomain setUrl: url];

	base			= [[apiDomain urlByAppendingSlug: BASE_SLUG] retain];
	search			= [[base urlByAppendingSlug: SEARCH_SLUG] retain];
	serviceReviews	= [[base urlByAppendingSlug: SERVICE_REVIEWS_SLUG] retain];
	serviceDetails	= [[base urlByAppendingSlug: SERVICE_DETAILS_SLUG] retain];
	serviceMedia	= [[base urlByAppendingSlug: SERVICE_MEDIA_SLUG] retain];
	categories		= [[base urlByAppendingSlug: CATEGORIES_SLUG] retain];
	attributes		= [[base urlByAppendingSlug: ATTRIBUTES_SLUG] retain];
	userCheckin		= [[base urlByAppendingSlug: CHECKIN_USER_SLUG] retain];
	serviceCheckin	= [[base urlByAppendingSlug: CHECKIN_SERVICE_SLUG] retain];
	users			= [[base urlByAppendingSlug: USERS_SLUG] retain];
	userServices	= [[base urlByAppendingSlug: USER_SERVICES_SLUG] retain];
	contacts		= [[base urlByAppendingSlug: CONTACTS_SLUG] retain];
	userActivity	= [[base urlByAppendingSlug: USER_ACTIVITY_SLUG] retain];
	socialActivity	= [[base urlByAppendingSlug: SOCIAL_ACTIVITY_SLUG] retain];
	userLists		= [[base urlByAppendingSlug: LISTS_SLUG] retain];
	userTags		= [[base urlByAppendingSlug: TAGS_SLUG] retain];
	servicePage		= [[base urlByAppendingSlug: PRO_SLUG] retain];
    offers          = [[base urlByAppendingSlug: OFFERS_SLUG] retain];
    userOffers      = [[base urlByAppendingSlug: OFFERS_USER_SLUG] retain];
    siteOffers      = [[base urlByAppendingSlug: OFFERS_SITE_SLUG] retain];
}

static ApiUrlProvider *instance;

+ (ApiUrlProvider *) provider
{
	if (!instance) {
		instance = [[ApiUrlProvider alloc] init];
		[instance build];
	}
	return instance;
}

- (void) setDomain:(NSString *) domain
{
	baseHost = [domain retain];
	[self build];
}

- (void) setSubDomain:(NSString *) subDomain
{
	baseSubHost = [subDomain retain];
	[self build];
}

- (void) setProtocol:(NSString *) protocol
{
	baseProtocol = protocol;
	[self build];
}

- (void)dealloc
{
    [baseProtocol release];
    [baseHost release];
    [baseSubHost release];
	
	[base release];
    [baseDomain release];
    [servicePage release];
    [apiDomain release];
	
	[search release];
    [serviceReviews release];
    [serviceDetails release];
    [serviceMedia release];
	[categories release];
    [attributes release];
    [users release];
    [userServices release];
    [contacts release];
	[userActivity release];
    [socialActivity release];
    [offers release];
    [userOffers release];
    [siteOffers release];
    
	[userCheckin release];
    [serviceCheckin release];
    [userLists release];
    [userTags release];
    [super dealloc];
}

@end
